﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Xml;
using System.Xml.Serialization;

namespace LevelEditor.Document
{
    [Serializable]
    public class Layer
    {
        #region Properties

        /// <summary>
        /// True = layer is drawn
        /// </summary>
        [XmlAttribute]
        public bool Visible { get; set; }

        /// <summary>
        /// How much to scale camera position for parallax effect [default is (1,1)]
        /// </summary>
        public PointF ParallaxScale { get; set; }

        /// <summary>
        /// Size of this layer in number of tiles
        /// </summary>
        public Size Size { get; set; }

        /// <summary>
        /// List of tile sets to use for this layer
        /// </summary>
        public TileSet TileSet { get; set; }

        /// <summary>
        /// List of tiles in this layer
        /// </summary>
        public List<uint> Tiles { get; set; }

        #endregion

        #region Constructors

        public Layer()
        {
            Size = new Size(128, 32);
            Visible = true;
            ParallaxScale = new PointF(1,1);
            TileSet = null;
            Tiles = null;
        }

        public Layer(Layer copy)
        {
            Size = copy.Size;
            Visible = copy.Visible;
            ParallaxScale = copy.ParallaxScale;
            TileSet = copy.TileSet;
            Tiles = new List<uint>(copy.Tiles);
        }

        #endregion

        #region Interface Methods

        public void Resize(Size sizeInTiles)
        {
            if (sizeInTiles != Size || Tiles.Count == 0)
            {
                List<uint> OldTiles = Tiles;
                Tiles = new List<uint>();
                for (int y = 0; y < sizeInTiles.Height; ++y)
                {
                    for (int x = 0; x < sizeInTiles.Width; ++x)
                    {
                        if (x >= Size.Width || y >= Size.Height || OldTiles == null || OldTiles.Count == 0)
                            Tiles.Add(0);
                        else
                            Tiles.Add(OldTiles[x + y * Size.Width]);
                    }
                }
                Size = sizeInTiles;
            }
        }

        #endregion

    }
}
